#include <stdio.h>
#include <stdint.h>

#include "i2c_base.h"
#include "dw_iic.h"

dw_iic_handle_t g_handle_tf = NULL;
dw_iic_handle_t g_handle_bklgt_pwr = NULL;

int lcd_i2c_int(void)
{
    if (g_handle_tf == NULL) {
        g_handle_tf = dw_i2c_init(LCD_TF_IIC_INDEX, DW_IIC_BUS_SPEED_STANDARD, NULL);
        if (g_handle_tf == NULL) {
            printf("dw_i2c_init tf error\n");
            return -1;
        }
    }
    if (g_handle_bklgt_pwr == NULL) {    
        g_handle_bklgt_pwr = dw_i2c_init(LCD_BL_PWR_IIC_INDEX, DW_IIC_BUS_SPEED_FAST, NULL);
        if (g_handle_bklgt_pwr == NULL) {
            printf("dw_i2c_init bl and power error\n");
            return -1;
        }
    }

    return 0;
}

/* Use iic to write data to camera register */
int32_t lcd_i2c_write(dw_iic_handle_t handle, uint8_t slave_addr,
    uint16_t reg, uint8_t val)
{
    int32_t ret;

    ret = dw_i2c_write(handle, slave_addr, reg, 1, &val, 1);
    if (ret < 0) {
        printf("dw_i2c_read error.\n");
        return -1;
    }

    return 0;
}

/* Use iic to read data from camera register */
int32_t lcd_i2c_read(dw_iic_handle_t handle, uint8_t slave_addr,
    uint16_t reg, uint8_t *read_data)
{
    int32_t ret;

    ret = dw_i2c_read(handle, slave_addr, reg, 1, read_data, 1);
    if (ret < 0) {
        printf("dw_i2c_read error.\n");
        return -1;
    }

    return 0;
}

